﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.EHR.Library;
using ZX.BLL;
using ZX.Common;
using System.Data;
using ZX.SPL.ORM;
using System.Reflection;


namespace ZX.BLL.ClientMobi.Message
{
    public class BridgeInstanceBase : MessagePlyBase
    {
        public override int Save(object o1, object o2)
        {
            StringBuilder InsertInitSql = new StringBuilder();
            DataTable tabTemp = new DataTable();
            DatSysNewsMonthInfo news = o1 as DatSysNewsMonthInfo;

            IDictionary<int, object> newsInfoDic = o2 as IDictionary<int, object>;

            if (news == null && newsInfoDic == null) return 0;

            if (string.IsNullOrEmpty(news.ID))
                news.ID = Guid.NewGuid().ToString();

            int ClassID = 0;
            //tabTemp = new DAL.DBExtend.DbSql().ExecuteQuery("select CodeNO From Code_BaseInfo where CodeName='反馈建议'");
            //if (tabTemp != null && tabTemp.Rows.Count > 0)
            //{
            //    ClassID = Convert.ToInt32(tabTemp.Rows[0]["CodeNO"]);
            //}

            //重载写入时间
            news.CreateDate = DateTime.Now;
            string columns = "";
            string columnsValue = "";

            Type t = news.GetType();
            PropertyInfo[] perperty1 = t.GetProperties();
            foreach (var property in perperty1)
            {
                if (property.Name == "ChildCX" || property.Name == "MsgReplyInstance") continue;

                columns += ((!string.IsNullOrEmpty(columns)) ? "," : "") + "[" + property.Name + "]";
                if (property.Name == "ISDelete")
                    columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "0";
                else if (property.Name == "Classify")
                    columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "'" + ClassID + "'";
                else
                    columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "'" + property.GetGetMethod().Invoke(news, null) + "'";
            }

            InsertInitSql.AppendFormat("Insert into [DatSysNewsMonth] ({0}) values ({1}) \n", columns, columnsValue);

            //-------------------------------------------------------------------------------------------------------------

            //获取接收人userid (固定)
            #region Reset Varrible
            tabTemp = null;
            columns = "";
            columnsValue = "";
            #endregion


            string fusID = string.Empty;

            tabTemp = new DAL.DBExtend.DbSql().ExecuteQuery("select top 1 UserID From DatUser where UName like '赵云娇%'");
            if (tabTemp != null && tabTemp.Rows.Count > 0) fusID = tabTemp.Rows[0]["UserID"].ToString();

            DatSysNewsMonthInfoInfo infoinfo = null;
            if (newsInfoDic.Count == 1)
            {
                infoinfo = newsInfoDic[1] as DatSysNewsMonthInfoInfo;
                if (infoinfo != null)
                {
                    infoinfo.NID = news.ID;
                    infoinfo.ReadDate = DateTime.Now;
                    PropertyInfo[] propertyList = infoinfo.GetType().GetProperties();
                    foreach (var property in propertyList)
                    {
                        columns += ((!string.IsNullOrEmpty(columns)) ? "," : "") + "[" + property.Name + "]";
                        if (property.Name == "IsDelete")
                            columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "0";
                        else if (property.Name == "RecevePeopleID")
                            columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "'" + fusID + "'";
                        else if (property.Name == "AddTime")
                            columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "NULL";
                        else if (property.Name == "ReadDate")
                            columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "NULL";
                        else
                            columnsValue += ((!string.IsNullOrEmpty(columnsValue)) ? "," : "") + "'" + property.GetGetMethod().Invoke(infoinfo, null) + "'";

                    }
                    InsertInitSql.AppendFormat("Insert into [DatSysNewsMonthInfo] ({0}) values ({1})", columns, columnsValue);

                }

            }

            return new DAL.DBExtend.DbSql().ExecuteScalar(InsertInitSql.ToString());

        }

        public override void ReplyMsg()
        {
            base.ReplyMsg();
        }

        public override string GetList()
        {
            return base.GetList();
        }

        public override object GetListByTSQL(IDictionary<string, object> paramsList)
        {
            DataTable table = new DataTable();
            if (paramsList.ContainsKey("t-sql"))
            {
                if (paramsList.ContainsKey("action"))
                    return new DAL.DBExtend.DbSql().ExecuteScalar(paramsList["t-sql"].ToString());
                else if (paramsList.ContainsKey("acrtion"))
                    return new DAL.DBExtend.DbSql().ExecuteSql(paramsList["t-sql"].ToString());
                else
                    table = new DAL.DBExtend.DbSql().ExecuteQuery(paramsList["t-sql"].ToString());

            }

            return table;
        }

    }
}
